<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>

  <body>
    <div id="app"></div>

    <template id="my-app">
      <button @click="changeFullName">修改FullName</button>
      <h2>{{fullName}}</h2>
    </template>

    <script src="../js/Vue.js"></script>
    <script>
      const app = {
        template: '#my-app',
        data() {
          return {
            firstName: 'kobe',
            lastName: 'Bryant'
          }
        },
        computed: {
          // fullName 的 getter 方法
          // fullName() {
          // 	console.log('computed的fullName');
          // 	return this.firstName + ' ' + this.lastName
          // },

          // fullName 的getter和setter方法
          // 给计算属性赋值会调用set方法，值是set方法的形参1
          fullName: {
            get() {
              return this.firstName + ' ' + this.lastName
            },
            set(newValue) {
              console.log(newValue)
              const names = newValue.split(' ')
              this.firstName = names[0]
              this.lastName = names[1]
            }
          }
        },
        methods: {
          changeFullName() {
            this.fullName = 'Coder why'
          }
        }
      }

      Vue.createApp(app).mount('#app')
    </script>
  </body>
</html>
